//
//  iSeeNetworkRequest.m
//  iSee
//
//  Created by 刘琛 on 2017/11/10.
//  Copyright © 2017年 刘琛. All rights reserved.
//

#import "iSeeNetworkRequest.h"
#import <MJExtension/MJExtension.h>
#import <AFNetworking/AFNetworking.h>
#import "ISeeUserInfoModel.h"


#define kNMarketSuccessCode    200

@implementation iSeeNetworkRequest

+ (void)getUrl:(NSString *) url params:(NSDictionary *) params success:(void (^)(id object))success failure:(void (^)(NSError *error))failure {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    NSDictionary *pamar = [NSDictionary dictionaryWithObjectsAndKeys:params,@"body", nil];
    NSString *urlStr = [[NSString stringWithFormat:@"%@?pdata=%@", url, [pamar mj_JSONString]] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    [manager GET:urlStr parameters:[NSDictionary dictionary] progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (success && code == kNMarketSuccessCode) {
            success(responseObject);
        } else if(failure) {
            NSLog(@"%@",task.originalRequest);
            NSLog(@"%@",responseObject);
            failure([NSError errorWithDomain:NMarketMainUrl code:code userInfo:responseObject]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}


+ (void)postUrl:(NSString *) url params:(NSDictionary *) params success:(void (^)(id object))success failure:(void (^)(NSError *error))failure {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    NSString *param = [NSString string];
    
    if (params.count) {
        NSDictionary *pamar = [NSDictionary dictionaryWithObjectsAndKeys:params,@"body", nil];
        param = [NSString stringWithFormat:@"pdata=%@",[pamar mj_JSONString]];
    }
    
    [manager POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (success && code == kNMarketSuccessCode) {
            success(responseObject);
        } else if(failure) {
            NSLog(@"%@",task.originalRequest);
            failure([NSError errorWithDomain:NMarketMainUrl code:code userInfo:responseObject]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)getWithHeaderUrl:(NSString *) url params:(NSDictionary *) params success:(void (^)(id object))success failure:(void (^)(NSError *error))failure {

    
    NSString *seccion = [[NSUserDefaults standardUserDefaults] objectForKey:NMarketUserSession];
    
    ISeeUserInfoModel *userInfo = [ISeeUserInfoModel mj_objectWithKeyValues:[[[NSUserDefaults standardUserDefaults] objectForKey:NMarketUserInfo] mj_JSONObject]];
    
    NSMutableDictionary *headerDic = [NSMutableDictionary dictionary];
    [headerDic setValue:seccion forKey:@"session"];
//    [headerDic setValue:self.devicetoken forKey:HCGFReqDeviceToken];
    [headerDic setValue:[[[NSBundle mainBundle] infoDictionary]objectForKey:@"CFBundleShortVersionString"] forKey:@"version"];
    [headerDic setValue:@"IOS" forKey:@"platform"];
    [headerDic setValue:[NSString stringWithFormat:@"%@"
                         ,userInfo.userId] forKey:@"userId"];
    [headerDic setValue:[[UIDevice currentDevice] systemVersion] forKey:@"platformVersion"];

    NSDictionary *package = [NSDictionary dictionaryWithObjectsAndKeys:headerDic, @"header",params,@"body",nil];
    
    NSString *urlStr = [NSString string];
    NSString *pdata = [package mj_JSONString];
    

    urlStr = [[NSString stringWithFormat:@"%@?pdata=%@",url, pdata] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlStr parameters:package progress:^(NSProgress * _Nonnull downloadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (success && code == kNMarketSuccessCode) {
            success(responseObject);
        } else if(failure) {
            NSLog(@"%@",task.originalRequest);
            failure([NSError errorWithDomain:NMarketMainUrl code:code userInfo:responseObject]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            NSLog(@"%@",task.originalRequest);
            failure(error);
        }
    }];
}

+ (void)postWithHeaderUrl:(NSString *) url params:(NSDictionary *) params success:(void (^)(id object))success failure:(void (^)(NSError *error))failure {
    
    NSString *seccion = [[NSUserDefaults standardUserDefaults] objectForKey:NMarketUserSession];
    
    ISeeUserInfoModel *userInfo = [ISeeUserInfoModel mj_objectWithKeyValues:[[[NSUserDefaults standardUserDefaults] objectForKey:NMarketUserInfo] mj_JSONObject]];
    
    NSMutableDictionary *headerDic = [NSMutableDictionary dictionary];
    [headerDic setValue:seccion forKey:@"session"];
    //    [headerDic setValue:self.devicetoken forKey:HCGFReqDeviceToken];
    [headerDic setValue:[[[NSBundle mainBundle] infoDictionary]objectForKey:@"CFBundleShortVersionString"] forKey:@"version"];
    [headerDic setValue:@"IOS" forKey:@"platform"];
    [headerDic setValue:[NSString stringWithFormat:@"%@"
                         ,userInfo.userId] forKey:@"userId"];
    [headerDic setValue:[[UIDevice currentDevice] systemVersion] forKey:@"platformVersion"];
    
    NSDictionary *package = [NSDictionary dictionaryWithObjectsAndKeys:headerDic, @"header",params,@"body",nil];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:url parameters:package progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (success && code == kNMarketSuccessCode) {
            success(responseObject);
        } else if(failure) {
            NSLog(@"%@",task.originalRequest);
            failure([NSError errorWithDomain:NMarketMainUrl code:code userInfo:responseObject]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            NSLog(@"%@",task.originalRequest);
            failure(error);
        }
    }];
}


@end
